package com.atguigu.gmall.oms.listener;

import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.atguigu.gmall.ums.entity.UserBoundVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import jdk.nashorn.internal.parser.Token;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SpuInfoListener {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "oms_fair_queue",durable = "true")
            ,exchange =@Exchange(
                    value = "ORDER_CREATE_EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
        key = "oms.useless"))
    private void listener(String orderToken, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        this.orderMapper.updateByToken(orderToken,0,5);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "order_death_queue")
    private void closeOrder(String orderToken, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        if (this.orderMapper.updateByToken(orderToken,0,4)==1){
            this.rabbitTemplate.convertAndSend("ORDER_CREATE_EXCHANGE","stock.unlock",orderToken);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "oms_pay_queue",durable = "true")
            ,exchange =@Exchange(
            value = "ORDER_PAY_EXCHANGE",
            ignoreDeclarationExceptions = "true",
            type = ExchangeTypes.TOPIC),
            key = {"pay.success"}))
    public void paySuccess(String orderToken, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
       if (this.orderMapper.updateByToken(orderToken,0,1)==1){
           //如果修改订单状态成功，则减库存
            //todo wms减库存
           this.rabbitTemplate.convertAndSend("ORDER_PAY_EXCHANGE","stock.minus",orderToken);
           channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
       }
       channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
    }


}
